/****************************************************************
 * Copyright (c) 2006-2007 Besture All Rights Reserved.
 * 
 * File Name:		ScriptUtil.cs
 * 
 * Created:		2013/06/24
 * Original Author:	Hongwei Zhao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace Besture.Common.PageControl.Script
{
    ///<summary>
    /// Script util
    ///</summary>
    public class ScriptUtil
    {
        ///<summary>
        /// Get the script to open a modal dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<remarks></remarks>
        public static string GetOpenModalDialogScript(string strUrl, int intWith, int intHeight)
        {
            return ScriptUtil.GetOpenModalDialogScript(strUrl, intWith, intHeight, 1, 0, 0);
        }

        ///<summary>
        /// Get the script to open a modal dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<remarks></remarks>
        public static string GetModalDialogScript(string strUrl, int intWith, int intHeight, bool lbFront)
        {
            return ScriptUtil.GetModalDialogScript(strUrl, intWith, intHeight, 1, 0, 0, lbFront);
        }

        ///<summary>
        /// Get the script to open a modal dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<param name="intCenter">Center Flag(1:center 0:not center)</param>
        ///<param name="intHelp">Help Flag(1:with help 0:without help)</param>
        ///<param name="intStatus">Status Flag(1:with status bar 0:without status bar</param>
        ///<remarks></remarks>
        public static string GetOpenModalDialogScript(string strUrl,int intWith,int intHeight,int intCenter,int intHelp,int intStatus)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript' id='TestScript'>");
            strScript.Append("window.showModalDialog('");
            strScript.Append(strUrl);
            strScript.Append("',window,'DialogWidth:");
            strScript.Append(intWith);
            strScript.Append("px;DialogHeight:");
            strScript.Append(intHeight);
            strScript.Append("px;center=");
            strScript.Append(intCenter);
            strScript.Append(";help=");
            strScript.Append(intHelp);
            strScript.Append(";status=");
            strScript.Append(intStatus);
            strScript.Append(";');");
            strScript.Append("window.location.href=window.location.href;");
            strScript.Append("</script>");
            return strScript.ToString();
        }

        ///<summary>
        /// Get the script to open a modal dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<param name="intCenter">Center Flag(1:center 0:not center)</param>
        ///<param name="intHelp">Help Flag(1:with help 0:without help)</param>
        ///<param name="intStatus">Status Flag(1:with status bar 0:without status bar</param>
        ///<remarks></remarks>
        public static string GetModalDialogScript(string strUrl, int intWith, int intHeight, int intCenter, int intHelp, int intStatus, bool lbFront)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript' id='TestScript'>");
            strScript.Append("window.showModalDialog('");
            strScript.Append(strUrl);
            strScript.Append("',window,'DialogWidth:");
            strScript.Append(intWith);
            strScript.Append("px;DialogHeight:");
            strScript.Append(intHeight);
            strScript.Append("px;center=");
            strScript.Append(intCenter);
            strScript.Append(";help=");
            strScript.Append(intHelp);
            strScript.Append(";status=");
            strScript.Append(intStatus);
            strScript.Append(";');");
            if (lbFront)
            {
                strScript.Append("document.getElementById('btnReLoad').click();");
            }
            else
            {
                strScript.Append("document.getElementById('ctl00_MenuHolder_btnReLoad').click();");
            }            
            strScript.Append("</script>");
            return strScript.ToString();
        }

        ///<summary>
        /// Get the script to open a modeless dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<remarks></remarks>
        public static string GetOpenModelessDialogScript(string strUrl, int intWith, int intHeight)
        {
            return ScriptUtil.GetOpenModelessDialogScript(strUrl, intWith, intHeight, 1, 0, 0, 0, 0, 0);
        }
        ///<summary>
        /// Get the script to open a modeless dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<param name="intWith">With of dialog</param>
        ///<param name="intHeight">Height of dialog</param>
        ///<param name="intCenter">Center Flag(1:center 0:not center)</param>
        ///<param name="intHelp">Help Flag(1:with help 0:without help)</param>
        ///<param name="intStatus">Status Flag(1:with status bar 0:without status bar</param>
        ///<param name="intToolbar">Toolbar Flag(1:with tool bar 0:without tool bar</param>
        ///<param name="intMenubar">Status Flag(1:with menu bar 0:without menu bar</param>
        ///<param name="intLocation">Location Flag(1:with location 0:without Location</param>
        ///<remarks></remarks>
        public static string GetOpenModelessDialogScript(string strUrl, int intWith, int intHeight, int intCenter, int intHelp, int intStatus, int intToolbar, int intMenubar, int intLocation)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("window.open('");
            strScript.Append(strUrl);
            strScript.Append("',null,'width:");
            strScript.Append(intWith);
            strScript.Append("px;height:");
            strScript.Append(intHeight);
            strScript.Append("px;center=");
            strScript.Append(intCenter);
            strScript.Append(";help=");
            strScript.Append(intHelp);
            strScript.Append(";toolbar=");
            strScript.Append(intToolbar);
            strScript.Append(";menubar=");
            strScript.Append(intMenubar);
            strScript.Append(";location=");
            strScript.Append(intLocation);
            strScript.Append(";status=");
            strScript.Append(intStatus);
            strScript.Append(";');");
            strScript.Append("</script>");
            return strScript.ToString();
        }

        ///<summary>
        /// Get the script to open a modeless dialog;
        ///</summary>
        ///<param name="strUrl">Url</param>
        ///<remarks></remarks>
        public static string GetOpenModelessDialogScript(string strUrl)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("newwindow = window.open('");
            strScript.Append(strUrl);
            strScript.Append("','_parent','');");            
            strScript.Append("</script>");
            return strScript.ToString();
        }

        ///<summary>
        /// Get the script to close a modal dialog;
        ///</summary>
        ///<remarks></remarks>
        public static string GetCloseModalDialogScript()
        {
            return ScriptUtil.GetCloseModalDialogScript("");
        }
        ///<summary>
        /// Get the script to close a modal dialog;
        ///</summary>
        ///<param name="strReturnValue">return value</param>
        ///<remarks></remarks>
        public static string GetCloseModalDialogScript(string strReturnValue){
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("returnValue = '" + strReturnValue + "';");
            strScript.Append("window.opener=self;");
            strScript.Append("self.window.close();");
            strScript.Append("</script>");
            return strScript.ToString();
        }
        ///<summary>
        /// Get the script to close a modal dialog;
        ///</summary>
        ///<param name="strReturnValue">return value</param>
        ///<remarks></remarks>
        public static string GetCloseModelessDialogScript()
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("self.window.close();");
            strScript.Append("</script>");
            return strScript.ToString();
        }
        ///<summary>
        /// Get the script to display a message box;
        ///</summary>
        ///<param name="strMessage">displayed message</param>
        ///<remarks></remarks>
        public static string GetMessageBoxScript(string strMessage,string strFocusControlId)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("alert(\"" + strMessage + "\");");
            if (strFocusControlId != null && !strFocusControlId.Trim().Equals(""))
            {
                strScript.Append("var obj = document.getElementById('" + strFocusControlId + "');");
                strScript.Append("obj.focus();");
            }
            strScript.Append("</script>");
            return strScript.ToString();
        }
    }
}
